$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid_1: null,
        dataGrid_2: null,
        currentType: null,
        init: function () {
            this.dataGrid_1 = new xhDataGrid($('#typeList'), 'lm/goods/getGoodsType');
            this.dataGrid_1.parseToolbar().init().activeButton(['bt_type_del']);
            xhExtra.clearLoadSuccess(this.dataGrid_1);
            this.dataGrid_2 = new xhDataGrid($('#goodsList'), 'lm/goods/getGoodsData');
            this.dataGrid_2.parseToolbar().init().activeButton(['bt_goods_del']);
            xhExtra.clearLoadSuccess(this.dataGrid_2);
            this.listener();
        },
        listener: function () {
            var $this = this;
            //添加类型
            $('#bt_type_add').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().handleType(null);
            });
            //添加商品
            $('#bt_goods_add').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addGoods();
            });
            //删除类型
            $('#bt_type_del').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delType();
            });
            //添加商品
            $('#bt_goods_del').click(function(){
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delGoods();
            });
            //双击类型
            $this.dataGrid_1.onDblClickRow = function(rowIndex, rowData){
                var id = rowData['iId'];
                var sTypeName = rowData['sTypeName'];
                if (id.length <= 0) {
                    window.top.xhTop.showError('未选中记录！');
                    return false;
                }
                $this.funObj().handleType(id,sTypeName);
            };
            //双击物品
            $this.dataGrid_2.onDblClickRow = function(rowIndex, rowData){
                var id = rowData['iId'];
                if (id.length <= 0) {
                    window.top.xhTop.showError('未选中记录！');
                    return false;
                }
                window.top.xhTop.showModal({
                    title: '修改信息',
                    content: xhTool.getFrameString('lm/goods/handleGoodsData', {id:id}, 200),
                    button: '保存',
                    size: 'md',
                    width: '600',
                    backClose: false
                });
            };
            //选择类型
            $this.dataGrid_1.onSelect = function(rowIndex, rowData){
                $('#bt_type_del').linkbutton('enable');
                $this.currentType = rowData['iId'];
                $this.dataGrid_2.reload({iTypeId:rowData['iId']});
            };
        },
        funObj: function () {
            var $this = this;
            return {
                handleType: function (id, name) {
                    if (id === null){//创建
                        str = '<div><input id="typeName" class="swal2-input" placeholder="必填"></div>';
                        type = 1;
                        title = '新增类型';
                    } else {//修改
                        str = '<div><input id="typeName" class="swal2-input" placeholder="必填" value="' + name + '"></div>';
                        type = 2;
                        title = '修改类型';
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: title,
                        html: str,
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var typeName     = $('#typeName', window.top.document).val();
                                if (typeName < 1){
                                    reject('内容不能为空');
                                    return false;
                                } else {
                                    xhExtra.ajaxRequest('lm/goods/handleTypeData', {
                                        id: id,
                                        typeName: typeName,
                                        type: type
                                    }, {success: true});
                                    resolve();
                                    $this.dataGrid_1.reload();
                                }
                            })
                        }
                    })
                },
                addGoods: function () {
                    window.top.xhTop.showModal({
                        title: '新增物品',
                        content: xhTool.getFrameString('lm/goods/handleGoodsData?typeId='+$this.currentType, null, 200),
                        button: '保存',
                        size: 'md',
                        width: '600',
                        backClose: false
                    });
                },
                delType: function () {
                    var id = $this.dataGrid_1.getSelectValue('iId');
                    xhExtra.askChangeStatus('lm/goods/delTypeData', {id: id}, $this.dataGrid_1, '确认删除物品类型？', function (data) {
                        if (data.code === 200) {
                            $this.dataGrid_2.reload();
                        }
                    });
                },
                delGoods: function () {
                    var ids = $this.dataGrid_2.getSelectValues('iId');
                    xhExtra.askChangeStatus('lm/goods/delGoodsData', {ids: ids}, $this.dataGrid_2, '确认删除物品？');
                }
            }
        }
    }
};